package controller;

import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import management.ApplicationManager;
import model.Candidato;
import utilities.Utils;
import view.AddCandidateView;
import view.GenericView;

public class AddCandidateController implements GenericController {

	private ConfigurationController controller;
	private AddCandidateView view;
	
	public AddCandidateController(ConfigurationController controller) {
		this.controller = controller;
	}
	
	@Override
	public GenericView getView() {
		return view;
	}

	public void handleButtonAddPicture() {
		final JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fc.setAcceptAllFileFilterUsed(false);
		fc.addChoosableFileFilter (new FileFilter() {
			@Override
			public String getDescription() {
				return "Solo Archivos de imagen";
			}

			@Override
			public boolean accept(File f) {
				return true;
			}

		});
		int returnVal = fc.showOpenDialog((JFrame)this.getView());
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			if (isValidPath(file.getAbsolutePath())){
				((AddCandidateView)this.getView()).changeImage(file);
			} else {
				JDialog dialogo = new JDialog(); 
				JOptionPane.showMessageDialog(dialogo,"No se puede abrir este archivo", "Notificacion",
		                    JOptionPane.INFORMATION_MESSAGE);
			}
		}

	}

	private boolean isValidPath(String path) {
		return path.matches("(?si)^.*(?:jpg|png|jpeg)$");
	}

	public void handleButtonGuardar() {
		if (this.controller.getCandidateBeingModified() == null){
			String nombre = ((AddCandidateView)this.getView()).getTxtNombre().getText();
			String path = ((AddCandidateView)this.getView()).getPath();
			Integer id = ApplicationManager.getCurrentId();
			Utils.moveFile(path, id);
			Candidato c = new Candidato(nombre, id);
			if (((StartController)this.getParentController().getParentController()).getManager().addNewCandidate(c)){
				((ConfigurationController)this.getParentController()).notityChangeInCandidatesList();
			} else {
				JDialog dialogo = new JDialog(); 
				JOptionPane.showMessageDialog(dialogo,"Ya existe un usuario con ese nombre", "Notificacion",
			                    JOptionPane.INFORMATION_MESSAGE);
			}
		} else {
			JLabel lblImagen = ((AddCandidateView)this.getView()).getLblImagen();
			String nombre = ((AddCandidateView)this.getView()).getTxtNombre().getText();
			String path = ((AddCandidateView)this.getView()).getPath();
			this.controller.notifyChangeInCandidate(nombre,lblImagen,path);
		}
		this.getParentController().getView().showWindow();
		this.getView().hideWindow();
	}

	@Override
	public GenericController getParentController() {
		return this.controller;
	}

	public void createView() {
		this.view = new AddCandidateView();
		this.view.setController(this);
	}

	public void handleButtonCancelar() {
		this.getParentController().getView().showWindow();
		this.getView().hideWindow();
	}
	

}
